package zjc.bookkeeping.homepage.addpage;

import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import zjc.bookkeeping.R;
import zjc.bookkeeping.db.ConsumptionBean;
import zjc.bookkeeping.db.DBManager;
import zjc.bookkeeping.server.StaticData;
import zjc.bookkeeping.tools.KeyboardDialog;
import zjc.bookkeeping.tools.MethodDialog;
import zjc.bookkeeping.tools.RemarksDialog;
import zjc.bookkeeping.tools.TimeDialog;
import zjc.bookkeeping.tools.ToolsClass;

public class TransferFragment extends Fragment implements View.OnClickListener {
    ImageView consumptionClassIv;
    TextView moneyTv,consumptionClassTv,remarksTv,methodTv,businessTv,timeTv;
    Button sureBtn;
    ConsumptionBean consumptionBean;
    String method = StaticData.getMethodName();
    float money;
    int outid,inid;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        consumptionBean = new ConsumptionBean();
        consumptionBean.setConsumption_class_name("转账");
        consumptionBean.setConsumption_remarks("无备注");
        consumptionBean.setConsumption_money(0);
        consumptionBean.setaccount_id(DBManager.selectAccountId()+"");
        consumptionBean.setConsumption_kind(2);
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_transfer, container, false);
        initView(view);
        setNowTime();
        return view;
    }

    private void setNowTime() {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
        String time = simpleDateFormat.format(date);
        timeTv.setText(time);
        consumptionBean.setConsumption_time(time);
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        consumptionBean.setConsumption_year(year);
        consumptionBean.setConsumption_month(month);
        consumptionBean.setConsumption_day(day);
    }

    private void initView(View view) {
        consumptionClassIv = view.findViewById(R.id.transfer_iv_consumption_class);

        moneyTv = view.findViewById(R.id.transfer_tv_money);
        consumptionClassTv = view.findViewById(R.id.transfer_tv_consumption_class);
        methodTv = view.findViewById(R.id.transfer_tv_method);
        if (method != null){
            methodTv.setText(method);
            consumptionBean.setConsumption_method(method);
        }
        businessTv = view.findViewById(R.id.transfer_tv_business);
        remarksTv = view.findViewById(R.id.transfer_tv_remarks);
        timeTv = view.findViewById(R.id.transfer_tv_time);

        sureBtn = view.findViewById(R.id.transfer_btn_sure);

        moneyTv.setOnClickListener(this);
        methodTv.setOnClickListener(this);
        businessTv.setOnClickListener(this);
        remarksTv.setOnClickListener(this);
        timeTv.setOnClickListener(this);
        sureBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.transfer_tv_money:
                showMoneyDialog();
                break;
            case R.id.transfer_tv_method:
                getMethod();
                break;
            case R.id.transfer_tv_business:
                getBusiness();
                break;
            case R.id.transfer_tv_remarks:
                showRemarksDialog();
                break;
            case R.id.transfer_tv_time:
                showTimeDialog();
                break;
            case R.id.transfer_btn_sure:
                Log.i("method",consumptionBean.getConsumption_method() + "");
                Log.i("business",consumptionBean.getConsumption_business() + "");
                if (consumptionBean.getConsumption_method() == null  || consumptionBean.getConsumption_business() == null){
                    ToolsClass.showToast(getContext(),"请选择账户。",Toast.LENGTH_SHORT);
                    break;
                }
                if (consumptionBean.getConsumption_method().equals(consumptionBean.getConsumption_business())){
                    ToolsClass.showToast(getContext(),"账户不能相同。",Toast.LENGTH_SHORT);
                    break;
                }
                if (consumptionBean.getConsumption_money() == 0){
                    ToolsClass.showToast(getContext(),"请输入金额，且不能为0.", Toast.LENGTH_SHORT);
                }else {
                    DBManager.insertConsumption(consumptionBean);
                    DBManager.updateMethodOutmonry(money,outid);
                    DBManager.updateMethodInmonry(money,inid);
                    getActivity().finish();
                }
                break;
        }
    }

    private void showTimeDialog() {
        TimeDialog timeDialog = new TimeDialog(getContext());
        timeDialog.show();
        timeDialog.setOnTimeSureListener(new TimeDialog.OnTimeSureListener() {
            @Override
            public void onTimeSure(String time, int year, int month, int day) {
                timeTv.setText(time);
                consumptionBean.setConsumption_time(time);
                consumptionBean.setConsumption_year(year);
                consumptionBean.setConsumption_month(month);
                consumptionBean.setConsumption_day(day);
            }
        });
    }

    private void showRemarksDialog() {
        RemarksDialog remarksDialog = new RemarksDialog(getContext());
        remarksDialog.show();
        String str = remarksTv.getText().toString();
        remarksDialog.setRemarksEt(str);
        remarksDialog.setRemarksDialogSize();
        remarksDialog.setOnRemarksSureListener(new RemarksDialog.OnRemarksSureListener() {
            @Override
            public void onRemarksSure() {
                String str = remarksDialog.getRemarksEt();
                if (!TextUtils.isEmpty(str)){
                    remarksTv.setText(str);
                    consumptionBean.setConsumption_remarks(str);
                }

            }
        });
    }

    private void getBusiness() {
        MethodDialog methodDialog = new MethodDialog(getContext());
        methodDialog.show();
        methodDialog.setOnMethodItemListener(new MethodDialog.OnMethodItemListener() {
            @Override
            public void onMethodItemClick() {
                String str = methodDialog.getMethod();
                inid = methodDialog.getMethodId();
                businessTv.setText(str);
                consumptionBean.setConsumption_business(str);
            }
        });
    }

    private void getMethod() {
        MethodDialog methodDialog = new MethodDialog(getContext());
        methodDialog.show();
        methodDialog.setOnMethodItemListener(new MethodDialog.OnMethodItemListener() {
            @Override
            public void onMethodItemClick() {
                String str = methodDialog.getMethod();
                outid = methodDialog.getMethodId();
                methodTv.setText(str);
                consumptionBean.setConsumption_method(str);
            }
        });
    }

    private void showMoneyDialog() {
        KeyboardDialog keyboardDialog = new KeyboardDialog(getContext());
        keyboardDialog.show();
        keyboardDialog.setKeyboardDialogSize();
        keyboardDialog.setOnKeyboardSureListener(new KeyboardDialog.OnKeyboardSureListener() {
            @Override
            public void onKeyboardSure() {
                String str = keyboardDialog.getMoneyTv();
                if (!TextUtils.isEmpty(str)){
                    moneyTv.setText(str);
                    money = Float.parseFloat(str);
                    consumptionBean.setConsumption_money(money);
                }
            }
        });
    }
}